package cn.jesin.css.controller;

import cn.hutool.core.util.StrUtil;
import cn.jesin.css.common.Flag;
import cn.jesin.css.service.TagService;
import cn.jesin.css.vo.Page;
import cn.jesin.css.vo.responseformat.Code;
import cn.jesin.css.vo.responseformat.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/21 下午3:40
 */
@Tag(name = "标签控制器")
@RestController
public class TagController {

    @Resource
    private TagService tagService;

    /**
     * 分页获取所有标签
     */
    @Operation(summary = "分页获取所有标签")
    @GetMapping("/tags")
    public Result<Page<Tag>> getTags(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                     @RequestParam(value = "keyword", defaultValue = "") String keyword) {
        Page<Tag> tags = tagService.getTags(pageNum, pageSize, keyword);
        return Result.<Page<Tag>>builder().flag(Flag.SUCCESS.isCode()).code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo()).data(tags).build();
    }

    /**
     * 添加标签
     */
    @Operation(summary = "添加标签")
    @GetMapping("/add-tag")
    public Result<Tag> addTag(@RequestParam(value = "name") String name) {
        boolean res = tagService.addTag(name);
        return res ? Result.<Tag>builder().flag(Flag.SUCCESS.isCode()).code(Code.ADD_SUCCESS.getCode())
                .message(Code.ADD_SUCCESS.getInfo()).build() : Result.<Tag>builder().flag(Flag.FAIL.isCode())
                .code(Code.ADD_FAIL.getCode()).message(Code.ADD_FAIL.getInfo()).build();
    }


    /**
     * 删除标签
     */
    @Operation(summary = "删除标签")
    @DeleteMapping("/tag")
    public Result<Tag> deleteTag(@RequestParam(value = "id") Integer id) {
        boolean res = tagService.deleteTag(id);
        return res ? Result.<Tag>builder().flag(Flag.SUCCESS.isCode()).code(Code.DELETE_SUCCESS.getCode())
                .message(Code.DELETE_SUCCESS.getInfo()).build() : Result.<Tag>builder().flag(Flag.FAIL.isCode())
                .code(Code.DELETE_FAIL.getCode()).message(Code.DELETE_FAIL.getInfo()).build();
    }
}
